require 'rextral'

  # This is the active profiler source code.
  #  Using this one can profile data sources.
  #  The rules validator is where you can check ranges,types,dependencies,constraints etc.
  class ActiveProfiler
    
      def self.method_missing(method_name)
          puts method_name
          table_name = self
      end  
  
      def self.profile
      end

      def self.profile_columns(args_val)
            puts args_val
      end  

  end



  # This is the Rule validator.  
  # This method is for validating the range of multiple columns from a single table.  
  def validates_range_of(options={}) 
      connection = options[:connection]
      all_records = connection.select_all("select * from #{self.to_s.downcase}").size
      options.each{|key,value|
            if value != connection
                  if key.respond_to? "join"  
                        column_array = get_column_list(connection,self.to_s.downcase)  
                        all_columns = get_columns_from_numbers(key,column_array)
                        generate_range_compact_info(all_columns,connection,self.to_s.downcase,all_records,value)
                  elsif key.respond_to? "split"
                        puts "The statistical information"    
                        column_array = get_column_list(connection,self.to_s.downcase)  
                        all_columns = get_columns_from_positions(key,column_array)
                        generate_range_compact_info(all_columns,connection,self.to_s.downcase,all_records,value)
                  else    
                      
                  end  
            end  
      }
  end   


  # This method is for getting all the column names from the table
  def get_column_list(connection,table_name)
      column_array = []  
      all_columns = connection.execute("desc #{table_name};")
      all_columns.each{|val|
            column_array << val[0]
      }
      return column_array
  end  


  # This method checks if a given column follows the given range.
  def generate_range_compact_info(all_columns,connection,table_name,all_records,value)
        for column in all_columns
            out_of_bound = 0
            connection.select_all("select #{column} from #{table_name}").each{|col_value|
                  if !col_value.nil?
                        if !value.include? col_value[0]
                            out_of_bound = out_of_bound + 1
                        end   
                  end        
            }
            if out_of_bound != 0
                  puts "for #{column}, #{(out_of_bound/all_records)*100} % rows have values out of bounds"
            end    
        end
  end
 
 
  def validates_type_of(options={})
  end  


  def validates_type_of(options={})
  end  


  def validates_dependency_on(table_name)
  end


  def validates_confirmation_with(file_name)
  end  


  def validates_constraints_on(options={})
  end  


  def map_with(tab_array)
      $tab_array = tab_array
  end